#include "Console.h"

Console::Console(ConcuShare* concuShare): concuShare(concuShare) { }

void Console::show_shell() {

	std::cout << std::endl;
	std::cout << PROMPT;
	std::string operation= "";
	std::getline(std::cin, operation);
	std::string function= get_function(operation);

	if((function.compare(HELP_SHORT)) == 0 || (function.compare(HELP_LONG)) == 0)
		show_help();
	else {
	if((function.compare(VERSION_SHORT)) == 0 || (function.compare(VERSION_LONG)) == 0)
		show_version();
	else {
	if((function.compare(FILES_SHORT)) == 0 || (function.compare(FILES_LONG)) == 0)
		show_files_shared();
	else {
	if((function.compare(SHARE_SHORT)) == 0 || (function.compare(SHARE_LONG)) == 0)
		share_file(operation);
	else {
	if((function.compare(MY_FILES_SHORT)) == 0 || (function.compare(MY_FILES_LONG)) == 0)
		show_my_files_shared();
	else {
	if((function.compare(UNSHARE_SHORT)) == 0 || (function.compare(UNSHARE_LONG)) == 0)
		unshare_file(operation);
	else {
	if((function.compare(DOWNLOAD_SHORT)) == 0 || (function.compare(DOWNLOAD_LONG)) == 0)
		download_file(operation);
	else {
	if((function.compare(QUIT_SHORT)) == 0 || (function.compare(QUIT_LONG)) == 0)
		quit();
	else
		show_error("La opcion elegida no existe.");
							}
						}
					}
				}
			}
		}
	}
}

void Console::show_inicio() {
	std::cout << "()()  #################################################" << std::endl;
	std::cout << "(..) (``•.¸(``•.¸(``•.¸(``•.¸ ¸.•`´)¸.•`´)¸.•`´)¸.•`´)" << std::endl;
	std::cout << "C()() «´¨¸¸¸¸[ -~~~~~~~~~ ConcuShare ~~~~~~~~~-]¸¸¸¸¨`» ()*()" << std::endl;
	std::cout << "      (¸.•`´(¸.•`´(¸.•`´(¸.•`´ ``•.¸)``•.¸)``•.¸)``•.¸) (*'.')" << std::endl;
	std::cout << "      ################################################# (/(o)(o)..." << std::endl;
	std::cout << std::endl;
}

void Console::show_help() {
	std::cout << "Usos: " << std::endl;
	std::cout << "Opciones:" << std::endl;
	std::cout << "  -h,   --help      Imprime la informacion de uso." << std::endl;
	std::cout << "  -v,   --version   Imprime la version del programa." << std::endl;
	std::cout << "  -f,   --files     Muestra los archivos que se pueden descargar" << std::endl;
	std::cout << "  -s,   --share     Habilita un archivo para compartir." << std::endl;
	std::cout << "                    Luego se escribe el path del archivo" << std::endl;
	std::cout << "  -myf, --myfiles   Muestra mis archivos compartidos enumerados" << std::endl;
	std::cout << "  -u,   --unshare   Deshabilita un archivo compartido." << std::endl;
	std::cout << "                    Luego se escribe el numero del archivo" << std::endl;
	std::cout << "  -d,   --download  Descarga el archivo seleccionado." << std::endl;
	std::cout << "                    Luego se escribe el numero del archivo" << std::endl;
	std::cout << "  -q,   --quit      Salir del programa." << std::endl;
}

void Console::show_version() {
	std::cout << "ConcuShare 1.0" << std::endl;
	std::cout << "Copyright (C) 2011 Free Software Foundation, Inc." << std::endl;
	std::cout << "Writen by Veronica Bukaczewski, Lucia Garbarini y Matias De Antoni." << std::endl;
}

void Console::show_files_shared() {
	std::cout << "#------ Archivos Compartidos ------#" << std::endl;
	std::cout << "# | Process ID | File Name" << std::endl;
	std::cout << "--------------------------" << std::endl;
	show_files(concuShare->get_files_shared());
}

void Console::share_file(std::string& operation) {
	concuShare->share_file(get_path(operation));
}

void Console::show_my_files_shared() {
	std::cout << "#------ Mis Archivos Compartidos ------#" << std::endl;
	std::cout << "# | File Name" << std::endl;
	std::cout << "-------------" << std::endl;
	show_files(concuShare->get_my_files_shared());
}

void Console::unshare_file(std::string& operation) {
	unsigned int num_file= get_my_file_num(operation);
	concuShare->unshare_file(num_file-1);
}

void Console::download_file(std::string& operation) {
	unsigned int num_file= get_my_file_num(operation);
	concuShare->download_file(num_file-1);
}

void Console::show_error(const std::string& error_message) {
	std::cout << "-- " << error_message << " --" << std::endl;
	show_help();
}

void Console::show_bye() {
	std::cout << "############################# GOOD-BYE #############################" << std::endl;
}

void Console::quit() {
	concuShare->stop_application();
}

std::string Console::get_function(std::string &operation) {
	std::size_t final_pos= operation.find(SPACE);
	return std::string(operation, 0, final_pos);
}

std::string Console::get_path(std::string &operation) {
	std::size_t first_pos= operation.find(SPACE);
	if(first_pos != std::string::npos)
		return std::string(operation, first_pos+1, operation.length());
	return "";
}

unsigned int Console::get_my_file_num(std::string &operation) {
	std::string str_file_num;
	unsigned int file_num= 0;
	std::size_t first_pos= operation.find(SPACE);
	if(first_pos != std::string::npos) {
		str_file_num= std::string(operation, first_pos+1, operation.length());
		file_num= atoi(str_file_num.c_str());
	}
	return file_num;
}

void Console::show_files(const std::list<std::string>& files) {
	std::list<std::string>::const_iterator it;
	it= files.begin();
	for(;it != files.end(); it++)
		std::cout << *it << std::endl;
}

void Console::show_waiting() {
	std::cout << "Waiting..." << std::endl;
}
